<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>最大正方形</title>
</head>
<body>
  <script>
    function fn(matrix) {
      // 求出最大的边长，然后平方即可
      let maxSide = 0;
      const m = matrix.length, n = matrix[0].length;
      // 默认值为0
      let dp = Array.from(Array(m), () => Array(n).fill(0));
      for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
          if (matrix[i][j] === '1') {
            if (i === 0 || j === 0) {
              dp[i][j] = 1
            } else {
              dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1
            }
            maxSide = Math.max(maxSide, dp[i][j])
          }
        }
      }
      return maxSide * maxSide
    }
    console.log(fn([['0', '1'], ['1', '0']]))
  </script>
</body>
</html>